﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
@model TaoPortal.Areas.Admin.Models.UserModel
<ul class="breadcrumb" style="margin-bottom: 7px;">
    <li><a href="#">首页</a> <span class="divider">/</span></li>
    <li><a href="#">用户管理</a> <span class="divider">/</span></li>
    <li class="active">用户列表</li>
</ul>
<form tabindex="0" class="form-horizontal" id="searchForm">
    <table style="margin-bottom: 5px; width: 100%">
        <tr>
            <td style="width: 66px; text-align: right">用户名称：</td>
            <td style="width: 165px;">@Html.TextBoxFor(x => x.Searcher.UserName, new { @class = "input-normal" })</td>
            <td style="width: 66px; text-align: right">移动电话：</td>
            <td style="width: 165px;">@Html.TextBoxFor(x => x.Searcher.Tel, new { @class = "input-normal" })</td>
            <td style="width: 66px; text-align: right">角色名称：</td>
            <td style="width: 210px;">
                <div id="RoleName">
                    @Html.HiddenFor(x => x.Searcher.RoleID)
                </div>
            </td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td style="width: 66px; text-align: right">电子邮件：</td>
            <td style="width: 165px;">@Html.TextBoxFor(x => x.Searcher.Email, new { @class = "input-normal" })</td>
            <td style="width: 66px; text-align: right">固定电话：</td>
            <td style="width: 165px;">@Html.TextBoxFor(x => x.Searcher.Tel, new { @class = "input-normal" })</td>
            <td style="width: 66px; text-align: right">创建日期：</td>
            <td style="width: 210px;">@Html.TextBoxFor(x => x.Searcher.StartDate, new { @class = "calendar" }) - @Html.TextBoxFor(x => x.Searcher.EndDate, new { @class = "calendar" })</td>
            <td style="text-align: left">
                <button class="button button-small" id="btnSearch" type="submit">搜索</button></td>
            <td style="text-align: right">
                <button class="button button-small button-primary" id="btnCreate" type="button">新建</button></td>
        </tr>
    </table>
</form>
<div id="grid">
</div>
<script type="text/javascript">
    var Grid = BUI.Grid,
    Store = BUI.Data.Store,
    columns = [
      { title: '用户名称', width: 150, sortable: false, dataIndex: 'UserName' },
      {
          title: '角色名称', width: 150, sortable: false, dataIndex: 'Role', renderer: function (value, obj) {
              if (value) {
                  var v = "";
                  for (var i = 0; i < value.length; i++) {
                      if (i == 0)
                          v += value[i].RoleName;
                      else
                          v += "," + value[i].RoleName;
                  }
                  return v;
              } else {
                  return null;
              }
          }
      },
      {
          title: '积分', width: 80, sortable: false, dataIndex: 'Integral'
      },
      {
          title: '电子邮件', width: 250, sortable: false, dataIndex: 'Email'
      },
      {
          title: '固定电话', width: 150, sortable: false, dataIndex: 'UserInfo', renderer: function (value, obj) {
              if (value)
                  return value.Tel;
              else
                  return null;
          }
      },
      {
          title: '移动电话', width: 150, sortable: false, dataIndex: 'UserInfo', renderer: function (value, obj) {
              if (value)
                  return value.Phone;
              else
                  return null;
          }
      },
      { title: '创建日期', width: 150, sortable: false, dataIndex: 'CreateTime' },
      { title: '用户状态', width: 80, sortable: false, dataIndex: 'Status' },
      {
          title: '操作', width: 110, sortable: false, dataIndex: 'ID',
          renderer: function (value, obj) {
              return '<span class="grid-command" onclick="EditPage(\'' + value + '\')">编辑</span><span class="grid-command" onclick="Delete(\'' + value + '\')">禁用</span>';
          }
      }
    ];
    //加载列表
    var store = new Store({
        url: '/Admin/UserManage/AjaxGetList',
        proxy: {
            limitParam: 'pageSize',
            method: 'post'
        },
        autoLoad: true,
        pageSize: 10
    }),
    grid = new Grid.Grid({
        render: '#grid',
        loadMask: true,
        forceFit: true,
        plugins: [Grid.Plugins.AutoFit],
        columns: columns,
        store: store,
        bbar: {
            pagingBar: true
        }
    });
    grid.render();

    var Select = BUI.Select;
    var data = eval('@Html.Raw(Model.RoleList)');
    var select = new Select.Select({
        render: '#RoleName',
        value: "",
        width: '192',
        items: data,
        //forceFit: false, //不强迫列表跟选择器宽度一致
        valueField: '#Searcher_RoleID'
    });
    select.render();


    //搜索
    var form = $('#searchForm')[0];
    $(form).on('submit', function (ev) {
        ev.preventDefault();
        var obj = BUI.FormHelper.serializeToObject(form);
        obj.pageIndex = 0;
        obj.start = 0;
        store.load(obj);
    });
    //日期控件
    BUI.use('bui/calendar', function (Calendar) {
        var datepicker = new Calendar.DatePicker({
            trigger: '.calendar',
            //showTime: true,
            //maxDate: '2014-01-01',
            //minDate: '2013-7-25',
            autoRender: true
        });
    });
    var Overlay = BUI.Overlay
    var dialog = null;
    //新建页面
    $('#btnCreate').click(function () {
        dialog = new Overlay.Dialog({
            title: '用户管理->新建页面',
            width: 700,
            closeAction: 'destroy',
            height: 500,
            buttons: [],
            bodyStyle: { 'padding': '0px' },
            bodyContent: '<iframe frameborder="0" style="width:690px;height:438px" src="/Admin/UserManage/AddUser"></iframe>'
        });
        dialog.show();
    })
    function EditPage(id) {
        dialog = new Overlay.Dialog({
            title: '用户管理->编辑页面',
            width: 700,
            closeAction: 'destroy',
            height: 500,
            buttons: [],
            bodyStyle: { 'padding': '0px' },
            bodyContent: '<iframe frameborder="0" style="width:690px;height:438px" src="/Admin/UserManage/EditUser/' + id + '"></iframe>'
        });
        dialog.show();
    }

    function Delete(id) {
        if (confirm('确定删除？')) {
            $.ajax({
                type: 'post',
                url: '/Admin/UserManage/DeleteUser/' + id,
                success: function () {
                    $('#btnSearch').trigger('click');
                }
            })
        }
    }
    function SubmitSuccess() {
        alert('提交成功！')
        dialog.close();
        $('#btnSearch').trigger('click');
    }
    function CloseWin() {
        dialog.close();
    }
</script>

